<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 发
  Date: 2020/10/9
  Time: 10:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    if(session.getAttribute("username")==null){
        //表示尚未登陆
        out.print("您尚未登陆，3秒后会跳转回登陆界面");
        response.sendRedirect("login.jsp");
    }

    out.print("欢迎来到个人主页！");

    Class.forName("com.mysql.jdbc.Driver");

    String url="jdbc:mysql://localhost:3306/book";//数据库连接地址
    //建立数据库连接
    Connection connection= DriverManager.getConnection(url,"root","root");//第一个root为数据库用户名，第二个rot为数据库密码

    String sql="select * from user where username=? and password=?";
    PreparedStatement ps= connection.prepareStatement(sql);

    ps.setString(1,request.getParameter("username"));
    ps.setString(2,request.getParameter("password"));

    //"insert into user(username,password,gender) values (?,?,?)";

    ResultSet rs = ps.executeQuery();

    while(rs.next()){
        out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
    }
/*
用Statement对象执行sql语句

    Statement stmt=connection.createStatement();

    //stmt.executeUpdate()
    String sql="select * from user where username='"+request.getParameter("username)+"'";
    ResultSet rs =stmt.executeQuery(sql);//执行查询的函数，返回结果集
    while(rs.next()){
        out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
    }
 */
%>
</body>
</html>
